define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'hotel_id', title: __('Hotel_id')},
                        // {field: 'company_id', title: __('Company_id')},
                        {field: 'hotel.hotel_name', title: __('Hotel.hotel_name'), operate: 'LIKE'},
                        {field: 'order_job_type', title: __('Order_job_type'), searchList: {"1":__('Order_job_type 1'),"2":__('Order_job_type 2'),"3":__('Order_job_type 3'),"4":__('Order_job_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'order_job_date', title: __('Order_job_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'user.username', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user_mobile', title: __('User_mobile'), operate: false},
                        {field: 'company.company_name', title: __('Company.company_name'), operate: 'LIKE'},
                        // {field: 'time_hour', title: __('Time_hour')},
                        // {field: 'duration_hour', title: __('Duration_hour')},
                        // {field: 'fix_hour', title: __('Fix_hour')},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'all_peice', title: __('All_peice'), operate:'BETWEEN'},
                        // {field: 'user_first', title: __('User_first')},
                        // {field: 'user_first_time', title: __('User_first_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user_first_location', title: __('User_first_location'), operate: 'LIKE'},
                        // {field: 'user_second', title: __('User_second')},
                        // {field: 'user_second_time', title: __('User_second_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user_second_location', title: __('User_second_location'), operate: 'LIKE'},
                        // {field: 'company_first', title: __('Company_first')},
                        // {field: 'company_first_time', title: __('Company_first_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'compay_first_location', title: __('Compay_first_location'), operate: 'LIKE'},
                        // {field: 'compay_second', title: __('Compay_second')},
                        // {field: 'company_second_time', title: __('Company_second_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'compay_second_location', title: __('Compay_second_location'), operate: 'LIKE'},
                        // {field: 'is_abnormal', title: __('Is_abnormal'), searchList: {"1":__('Is_abnormal 1'),"2":__('Is_abnormal 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'is_renewal', title: __('Is_renewal'), searchList: {"1":__('Is_renewal 1'),"2":__('Is_renewal 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'stop',
                                    text: __('停止'),
                                    title: __('停止'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    refresh: true,
                                    icon: 'fa fa-stop-circle-o',
                                    url: 'order/batch?act=3',
                                    confirm: '\n' +
                                        '即将停止该订单，请确认是否停止？',
                                    visible: function (row) {
                                        return row.status < 3
                                    },
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 批量操作 停止/恢复
            $(".btn-more").on("click", function () {
                let act = $(this).data('act');
                let data = table.bootstrapTable('getSelections');
                for(let i of data){
                    if(i.status >= 3){
                        Toastr.error('只有已报名，进行中的订单才可停止');
                        return false
                    }
                }
                let ids = data.map(x => {return x.id}).join(',')
                $.post({
                    url: 'order/batch?act='+act+'&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});